﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla.Data;

namespace Accounting.Library
{
    public class LoaiTienTeService
    {
        /// <summary>
        /// Tra ve ma tai khoan ngoai te
        /// </summary>
        /// <param name="ctx">Data access context</param>
        /// <param name="LoaiTienId">Ma loai tien te</param>
        /// <returns>Ma tai khoan ngoai te (= 0 neu khong tim thay)</returns>
        public static int MaTaiKhoanNgoaiTe(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int LoaiTienId) 
        {
            try
            {
                Accounting.DataAccess.DM_LoaiTienTe loaiTien = (from p in ctx.DataContext.DM_LoaiTienTes
                                                                where (p.Khoa == LoaiTienId)
                                                                select p).Single();

                if (loaiTien.TienSuDung != true)
                {
                    return loaiTien.DM_TaiKhoan2.Khoa;
                }
                else
                {
                    return 0;
                }
            }
            catch 
            {
                return 0;
            }
        }

        /// <summary>
        /// Tra ve ma tai khoan ngoai te
        /// </summary>
        /// <param name="ctx">Data access context</param>
        /// <param name="LoaiTienId">Ma loai tien te</param>
        /// <returns>Ma tai khoan ngoai te (= 0 neu khong tim thay)</returns>
        public static bool LaLoaiTienTeChinh(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, int LoaiTienId)
        {
            try
            {
                Accounting.DataAccess.DM_LoaiTienTe loaiTien = (from p in ctx.DataContext.DM_LoaiTienTes
                                                                where (p.Khoa == LoaiTienId)
                                                                select p).Single();

                return (bool)loaiTien.TienSuDung;              
            }
            catch
            {
                return false;
            }
        }
    }
}
